import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static java.lang.Thread.sleep;

public class ThreadPoolExample {
    /*测试10个人，每个人点击100次，项目是否崩溃*/
    public static void main(String[] args) {
        // 创建一个固定大小的线程池，池中有10个线程
        ExecutorService executor = Executors.newFixedThreadPool(10);

        // 在线程池中提交任务
        for (int i = 0; i < 10; i++) {
            Runnable worker = new WorkerThread("Task-" + i);
            executor.execute(worker);
        }

        // 关闭线程池
        executor.shutdown();
        while (!executor.isTerminated()) {
            // 等待所有任务完成
        }
        System.out.println("所有任务已完成");
    }

    static class WorkerThread implements Runnable {
        private String taskName;

        public WorkerThread(String taskName) {
            this.taskName = taskName;
        }

        @Override
        public void run() {
            System.out.println("开始执行任务: " + taskName);
            WebDriver webDriver = new ChromeDriver();
            webDriver.get("http://127.0.0.1:8080/blog_list.html");
            for (int i = 0; i < 100; i++) {
                webDriver.findElement(By.cssSelector("a[href='blog_list.html']")).click();
            }
            try {
                sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            webDriver.quit();
            System.out.println("任务完成: " + taskName);
        }
    }
}
